package com.inspur.icity.bus.base.core.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.inspur.icity.bus.base.core.geo.OnTheEarth;
import com.inspur.icity.bus.base.core.geo.WgsGcjConverter;
import com.inspur.icity.bus.base.core.geo.coords.ICoordinates;

import java.io.IOException;

public class OnTheEarthJsonSerializer extends JsonSerializer<OnTheEarth> {
    ObjectMapper mapper;

    public OnTheEarthJsonSerializer(){
        mapper = new ObjectMapper();
    }
    public OnTheEarthJsonSerializer(ObjectMapper mapper){
        this.mapper = mapper;
    }

    @Override
    public void serialize(OnTheEarth onTheEarth, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        ICoordinates point = WgsGcjConverter.wgs84ToGcj02(onTheEarth.getLat(), onTheEarth.getLng());
        onTheEarth.setLng(point.getLng());
        onTheEarth.setLat(point.getLat());
        mapper.writeValue(jsonGenerator, onTheEarth);
    }
}
